<template>
	<div style="display: contents;">
		<uni-easyinput  
		trim
		:inputBorder="false"
		:clearable='false'
		v-model="theValue" 
		:disabled="disabled" 
		@clear='clear' 
		@focus="focus" 
		@blur="blur"
		@input="input"
		:placeholder="placeholder"
		>
			<slot name="default"></slot>
		</uni-easyinput>
	</div>
</template>

<script>
	export default {
		name: 'mInput',
		components: {},
		model: {
			prop: 'value',
			event: 'blur',
		},
		props: {
			value:'',
			placeholder:{
				type: String,
				default: ''
			},
			negative:{
				type: Boolean,
				default: false
			},
			disabled:{
				type: Boolean,
				default: false
			},
			// 整数位
			num1: {
				type: Number,
				default: 0,
				validator: v => v >= 0
			},
			// 小数位
			num2: {
				type: Number,
				default: 2,
				validator: v => v >= 0
			},
			thousands: {
				type: Boolean,
				default: true
			},
			// 千位控制  1234.00 =》 1000.00
			thousandInteger: {
				type: Boolean,
				default: false
			},
		},
		data() {
			return {
				theValue: '',
				realValue: '',
				reg1: /,/g,
				reg2: /(\d)(?=(?:\d{3})+$)/g
			}
		},
		watch: {
			value(v) {
				// console.log(444,v)
				if (this.theValue !== v) {
					if (v === null || v === undefined) {
						this.realValue = 0
						this.theValue = 0
					} else {
						this.realValue = v
						this.theValue = this.format(v + '', this.thousands)
					}
				}
			}

		},
		mounted() {
			this.realValue = this.value
			this.theValue = this.format(this.value || '', true)
		},
		methods: {
			clear(){
				this.$emit('blur', '')
			},
			input(e){
				setTimeout(()=>{
					e = e.replace(/[^\0-9\.-]/g, '')
					e = e.replace("-","$#$").replace(/\-/g, "").replace("$#$", "-");
					e = e.replace(/\.{2,}/g, ".");
					e = e.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
					e = e.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')		
					let v = e.split('.')
					
					this.theValue = e
				},50)
				
			},
			focus($event) {
				let v = $event.target.value
				if (typeof v === 'string' || typeof v === 'number') {
					this.theValue = this.format(v + '')
				}
			},
			blur($event) {
				let v = $event.target.value
				if (this.thousandInteger) {
					// 如果小于一千 或者不是数字统一设置为0
					if (isNaN(Number(v))) {
						v = 0
					} else {
						if (Number(v) < 1000) {
							v = 0
						} else {
							// 大于一千的将个十百位置为0
							v = Math.floor(Number(v) / 1000) * 1000
						}
					}
				}
				if (isNaN(Number(v))) {
					this.theValue = this.format(v, true)
				} else {
					this.realValue = this.format(v)
					this.theValue = this.format(v, true)
				}
				this.$emit('blur', this.realValue)
			},
			format(v, flag) {
				v = v + ''
				if (v) {
					let [v1, v2] = v.split('.')
					v2 = v2 || ''
					if (this.num2) {
						const len = this.num2 - v2.length
						if (len > 0) {
							for (let i = 0; i < len; i++) {
								v2 += '0'
							}
						} else if (len < 0) {
							v2 = v2.slice(0, this.num2)
						}
						v2 = '.' + v2
					} else {
						v2 = ''
					}
					if (this.thousands) {
						// 需要千分符
						if (flag) {
							v1 = v1.replace(this.reg2, '$1,')
						} else {
							v1 = v1.replace(this.reg1, '')
							
						}
					}			
					
					v =  v1 + v2
					// 解决  -000.11 问题  -0 无法转换字符串拼接
					if(Number(v1)<1){
						v = Number(v)
					}
					
				
					
				}
				return v
			}
		},
	}
</script>
<style>
	
</style>
